package cn.com.bluemoon.daps.system.controller;

import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.domain.UserInfoHolder;
import cn.com.bluemoon.daps.system.entity.DapSystemMenuCollect;
import cn.com.bluemoon.daps.system.service.DapSystemMenuCollectService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "菜单收藏管理")
@RestController
@Slf4j
@RequestMapping("/menu/collect")
public class DapSystemMenuCollectController {

    @Autowired
    DapSystemMenuCollectService dapSystemMenuCollectService;

    @BmPermission(value = "menu:collect:list", name = "菜单列表查询", ignore = true)
    @GetMapping("list")
    @ApiOperation(value = "菜单列表查询")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = DapSystemMenuCollect.class)})
    public ResultBean<IPage<DapSystemMenuCollect>> getMenuList(@ApiParam(value = "每页显示的数量", example = "0") @RequestParam Integer pageSize,
                                                             @ApiParam(value = "当前页码数", example = "0") @RequestParam Integer pageNum,
                                                             @ApiParam("关键字") @RequestParam(required = false) String keyWord){

        IPage<DapSystemMenuCollect> page = dapSystemMenuCollectService.selectByPage(pageSize, pageNum, keyWord);
        return ResultBean.ok(page);

    }

    @BmPermission(value = "menu:collect:list", name = "菜单列表查询全部", ignore = true)
    @GetMapping("all")
    @ApiOperation(value = "菜单列表查询全部")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = DapSystemMenuCollect.class)})
    public ResultBean<List<DapSystemMenuCollect>> getMenuAll(){

        List<DapSystemMenuCollect> list = dapSystemMenuCollectService.getBaseMapper()
                .selectList(new LambdaQueryWrapper<DapSystemMenuCollect>().eq(DapSystemMenuCollect::getCreateBy, UserInfoHolder.getAccount())
                        .orderByDesc(DapSystemMenuCollect::getCreateTime));
        return ResultBean.ok(list);

    }

    @BmPermission(value = "menu:collect:add", name = "收藏菜单", ignore = true)
    @PostMapping("list")
    @ApiOperation(value = "收藏菜单")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = ResultBean.class)})
    public ResultBean systemMenuCollect(@RequestBody @Valid DapSystemMenuCollect collect){

        Boolean save = dapSystemMenuCollectService.menuCollect(collect);
        return ResultBean.ok(save ? "收藏成功" : "收藏失败");
    }

    @BmPermission(value = "menu:collect:delete", name = "删除", ignore = true)
    @DeleteMapping("delete")
    @ApiOperation(value = "删除收藏菜单(可批量)")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = Boolean.class)})
    public ResultBean<String> deleteDatasource (@ApiParam("收藏id") @RequestParam String id){

        return dapSystemMenuCollectService.deleteMenu(id);
    }
}
